home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / util / faxinfo.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  136 lines

  1. /*    $Header: /usr/people/sam/fax/util/RCS/faxinfo.c,v 1.17 1994/03/01 19:26:43 sam Rel $
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include "tiffio.h"
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <time.h>
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include "PageSize.h"
  35.  
  36. #include "port.h"
  37.  
  38. static int
  39. isFAXImage(TIFF* tif)
  40. {
  41.     u_short w;
  42.     if (TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &w) && w != 1)
  43.     return (0);
  44.     if (TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &w) && w != 1)
  45.     return (0);
  46.     if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &w) ||
  47.       (w != COMPRESSION_CCITTFAX3 && w != COMPRESSION_CCITTFAX4))
  48.     return (0);
  49.     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &w) ||
  50.       (w != PHOTOMETRIC_MINISWHITE && w != PHOTOMETRIC_MINISBLACK))
  51.     return (0);
  52.     return (1);
  53. }
  54.  
  55. static void
  56. sanitize(char* dst, const char* src, u_int maxlen)
  57. {
  58.     u_int i;
  59.     for (i = 0; i < maxlen-1 && src[i] != '\0'; i++)
  60.     dst[i] = (isascii(src[i]) && isprint(src[i]) ? src[i] : '?');
  61.     dst[i] = '\0';
  62. }
  63.  
  64. void
  65. main(int argc, char** argv)
  66. {
  67.     char* cp;
  68.     int npages, ok;
  69.     TIFF* tif;
  70.     float vres, w, h;
  71.     long pageWidth, pageLength;
  72.     char sender[80];
  73.     char date[80];
  74.     struct pageSizeInfo* info;
  75.  
  76.     if (argc != 2) {
  77.     fprintf(stderr, "usage: %s file.tif\n", argv[0]);
  78.     exit(-1);
  79.     }
  80.     printf("%s:\n", argv[1]);
  81.     TIFFSetErrorHandler(NULL);
  82.     TIFFSetWarningHandler(NULL);
  83.     tif = TIFFOpen(argv[1], "r");
  84.     if (tif == NULL) {
  85.     printf("Could not open (%s).\n", strerror(errno));
  86.     exit(0);
  87.     }
  88.     ok = isFAXImage(tif);
  89.     if (!ok) {
  90.     printf("Does not look like a facsimile?\n");
  91.     exit(0);
  92.     }
  93.     if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp))
  94.     sanitize(sender, cp, sizeof (sender));
  95.     else
  96.     strcpy(sender, "<unknown>");
  97.     printf("%11s %s\n", "Sender:", sender);
  98.     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &pageWidth);
  99.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &pageLength);
  100.     if (TIFFGetField(tif, TIFFTAG_YRESOLUTION, &vres)) {
  101.     short resunit = RESUNIT_NONE;
  102.     TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
  103.     if (resunit == RESUNIT_CENTIMETER)
  104.         vres *= 25.4;
  105.     } else
  106.     vres = 98;
  107.     npages = 0;
  108.     do {
  109.     npages++;
  110.     } while (TIFFReadDirectory(tif));
  111.     printf("%11s %u\n", "Pages:", npages);
  112.     if (vres == 98)
  113.     printf("%11s Normal\n", "Quality:");
  114.     else if (vres == 196)
  115.     printf("%11s Fine\n", "Quality:");
  116.     else
  117.     printf("%11s %.2f lines/inch\n", "Quality:", vres);
  118.     h = pageLength / (vres < 100 ? 3.85 : 7.7);
  119.     w = (pageWidth / 204.) * 25.4;
  120.     info = closestPageSize(w, h);
  121.     if (info)
  122.     printf("%11s %s\n", "Page:", getPageName(info));
  123.     else
  124.     printf("%11s %lu by %lu\n", "Page:", pageWidth, pageLength);
  125.     delPageSize(info);
  126.     if (!TIFFGetField(tif, TIFFTAG_DATETIME, &cp)) {
  127.     struct stat sb;
  128.     fstat(TIFFFileno(tif), &sb);
  129.     strftime(date, sizeof (date),
  130.         "%Y:%m:%d %H:%M:%S %Z", localtime(&sb.st_mtime));
  131.     } else
  132.     sanitize(date, cp, sizeof (date));
  133.     printf("%11s %s\n", "Received:", date);
  134.     exit(0);
  135. }
  136.